{
	UCE - cross-platform game engine written in delphi.
	Copyright (c) 2009, Shutko Vitaly

	This file is part of UCE.

	UCE is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	UCE is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
}

unit uInp;

interface

uses
  uMat;

const
  KEY_ESCAPE = 27;
  KEY_TAB = 9;
  KEY_BACKSPACE = 8;
  KEY_SPACE = 32;
  KEY_ENTER = 13;

  KEY_SHIFT = 16;
  KEY_CONTROL = 17;
  KEY_ALT = 18;

  KEY_LEFT = 37;
  KEY_UP = 38;
  KEY_RIGHT = 39;
  KEY_DOWN = 40;

  KEY_F1 =  112;
  KEY_F2 =  113;
  KEY_F3 =  114;
  KEY_F4 =  115;
  KEY_F5 =  116;
  KEY_F6 =  117;
  KEY_F7 =  118;
  KEY_F8 =  119;
  KEY_F9 =  120;
  KEY_F10 = 121;
  KEY_F11 = 122;
  KEY_F12 = 123;

  KEY_0 = 48;
  KEY_1 = 49;
  KEY_2 = 50;
  KEY_3 = 51;
  KEY_4 = 52;
  KEY_5 = 53;
  KEY_6 = 54;
  KEY_7 = 55;
  KEY_8 = 56;
  KEY_9 = 57;

  KEY_A = 65;
  KEY_B = 66;
  KEY_C = 67;
  KEY_D = 68;
  KEY_E = 69;
  KEY_F = 70;
  KEY_G = 71;
  KEY_H = 72;
  KEY_I = 73;
  KEY_J = 74;
  KEY_K = 75;
  KEY_L = 76;
  KEY_M = 77;
  KEY_N = 78;
  KEY_O = 79;
  KEY_P = 80;
  KEY_Q = 81;
  KEY_R = 82;
  KEY_S = 83;
  KEY_T = 84;
  KEY_U = 85;
  KEY_V = 86;
  KEY_W = 87;
  KEY_X = 88;
  KEY_Y = 89;
  KEY_Z = 90;

  MOUSE_L = 256;
  MOUSE_M = 257;
  MOUSE_R = 258;

type
	TInp = class
    keys, prevKeys: array[0..255 + 3] of Boolean;
    mPos, mPrevPos, mDt: TVec;

    procedure Update;

		function AnyKeyDowned: Boolean;
		function KeyDowned(key: Integer): Boolean;
		function KeyPressed(key: Integer): Boolean;
		function KeyReleased(key: Integer): Boolean;

		function MousePos: TVec;
		function MouseDt: TVec;
		function MouseRealPos: TVec;
		function MouseRealDt: TVec;
	end;

implementation

uses
{$IFDEF WIN32}
  Windows, uAppWin32,
{$ELSE}
  //
{$ENDIF}
  uVar;

procedure TInp.Update;
{$IFDEF WIN32}
var
  p: TPoint;
{$ENDIF}
begin
  Move(keys[0], prevKeys[0], SizeOf(keys));

{$IFDEF WIN32}
  GetCursorPos(p);
  ScreenToClient(TAppWin32(app).wnd, p);
  mPos.Set_(p.x, p.y);
{$ELSE}
  //
{$ENDIF}

  mDt := mPos.Sub(mPrevPos);
  mPrevPos := mPos;
end;

function TInp.AnyKeyDowned: Boolean;
var
  i: Integer;
begin
  Result := False;

  for i := 0 to High(keys) do
    if keys[i] then
    begin
      Result := True;
      Exit;
    end;
end;

function TInp.KeyDowned(key: Integer): Boolean;
begin
  Result := keys[key];
end;

function TInp.KeyPressed(key: Integer): Boolean;
begin
  Result := keys[key] and not prevKeys[key];
end;

function TInp.KeyReleased(key: Integer): Boolean;
begin
  Result := not keys[key] and prevKeys[key];
end;

function TInp.MousePos: TVec;
begin
  Result := MakeVec(
    mousePos.x * eng.conf.cw / eng.conf.w,
    mousePos.y * eng.conf.ch / eng.conf.h);
end;

function TInp.MouseDt: TVec;
begin
  Result := MakeVec(
    mDt.x * eng.conf.cw / eng.conf.w,
    mDt.y * eng.conf.ch / eng.conf.h);
end;

function TInp.MouseRealPos: TVec;
begin
  Result := mousePos;
end;

function TInp.MouseRealDt: TVec;
begin
  Result := mDt;
end;

end.
